// pages/my/addrEdit/addrEdit.js
import { areaList } from '@vant/area-data';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        form: {
            username: '',
            phone: '',
            code: '',
            adreDetail: '',
            area: ''
        },
        adressId: 0,//地址id
        fixed: 0,//是否是默认地址
        provinces: '',
        cities: '',
        counties: '',
        flag: true,
        areaList,
        addOrEdit: 0,
    },
    // 设为默认点击事件函数
    handleChange(e) {
        console.log(this.data.fixed)
        if (this.data.fixed == 1) {
            this.setData({
                fixed: 0
            });
        } else {
            this.setData({
                fixed: 1
            });
        }
    },
    //返回上一页
    back() {
        wx.navigateBack({
            data: 1
        })
    },
    //默认收获地址
    setDefaAdrees(event) {
        console.log(event.detail)
        this.setData({
            defa: event.detail
        });
    },
    //获取省市县数据
    dlogcan(e) {
        this.setData({
            flag: false
        })
    },
    dlogConf(e) {
        console.log(e)
        var arr = e.detail.values;
        var address = [];
        arr.forEach(element => {
            address.push(element.name)
        });
        console.log(address)
        this.setData({
            flag: true,
            "form.area": address,
            provinces: arr[0].name,
            cities: arr[1].name,
            counties: arr[2].name
        })
    },
    chosearea(e) {
        if (this.data.flag) {
            this.setData({
                flag: false
            })
        } else {
            this.setData({
                flag: false
            })
        }
    },
    //表单提交
    onChange(event) {
        // event.detail 为当前输入的值
        this.setData({
            form: event.detail.value
        }, () => {
            if (this.data.addOrEdit == 0) {
                this.addAdress();
            } else {
                this.editAdress();
            }
        })
    },
    //发送查询请求
    getAdress() {
        wx.request({
            url: 'http://localhost:8080/keep/address/' + this.data.adressId,
            method: 'GET',
            data: {
                id: this.data.adressId
            },
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
                this.setData({
                    "form.username": res.data.data.consignee,
                    "form.phone": res.data.data.phone,
                    "form.code": res.data.data.zipCode,
                    "form.adreDetail": res.data.data.fullAddress,
                    "form.area": res.data.data.provinces+res.data.data.cities+res.data.data.counties,
                    "fixed": res.data.data.fixed,//是否是默认地址
                    "provinces": res.data.data.provinces,
                    "cities": res.data.data.cities,
                    "counties": res.data.data.counties,
                })
            },
        });
    },
    //发送修改请求
    editAdress() {
        wx.request({
            url: 'http://localhost:8080/keep/address',
            method: 'PUT',
            data: {
                id: this.data.adressId,
                userId: getApp().globalData.loginfo.userId,
                consignee: this.data.form.username,
                phone: this.data.form.phone,
                zipCode: this.data.form.code,
                fullAddress: this.data.form.adreDetail,
                provinces: this.data.provinces,
                cities: this.data.cities,
                counties: this.data.counties,
                fixed: this.data.fixed
            },
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
                if (res.data.code == 200) {
                    wx.showToast({
                        title: '修改成功',
                    })
                }
            },
            complete: () => {
                wx.navigateBack({
                    data: 1
                })
            }
        });
    },
    //发送新增请求dui
    addAdress() {
        wx.request({
            url: 'http://localhost:8080/keep/address',
            method: 'POST',
            data: {
                userId: getApp().globalData.loginfo.userId,
                consignee: this.data.form.username,
                phone: this.data.form.phone,
                zipCode: this.data.form.code,
                fullAddress: this.data.form.adreDetail,
                provinces: this.data.provinces,
                cities: this.data.cities,
                counties: this.data.counties,
                fixed: this.data.fixed
            },
            header: {
                authorization: getApp().globalData.authorization
            },
            success: res => {
            },
            complete: () => {
                wx.navigateBack({
                    data: 1
                })
            }
        });
    },
    // 头部返回
    onClickLeft() {
        wx.showToast({ title: '点击返回', icon: 'none' });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log()
        if (options.id == null) {
            this.setData({
                addOrEdit: 0
            })
        } else {
            this.setData({
                addOrEdit: 1,
                adressId: options.id
            }, () => {
                this.getAdress();
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})